﻿using System;
using System.Xml;
using System.Collections;




namespace GPCSC.Webs
{
    /// <summary>
    /// XMLParse 的摘要说明。
    /// </summary>
    public class XMLParse
    {
        public XMLParse()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 根据xml文件的路径名和命令的名字解析出相应的SQL语句
        /// </summary>
        /// <param name="strSearchName">命令的名字</param>
        /// <param name="commandType">命令类型</param>
        /// <param name="strXMLPath">xml文件名字</param>
        /// <returns>SQL命令</returns>
        public string ParseXML(string strSearchName, ref string commandType, string strXMLPath)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(strXMLPath);
                XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes("SqlText");
                foreach (XmlNode node in nodes)
                {
                    if (strSearchName == node.Attributes["Name"].Value)
                    {
                        commandType = node.Attributes["CommandType"].Value;
                        return node.InnerText;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("xml解析失败！", ex);
            }
            return null;
        }

        /// <summary>
        /// 根据xml文件的路径名和命令的名字解析出相应的SQL语句
        /// </summary>
        /// <param name="strSearchName">命令的名字</param>
        /// <param name="strXMLPath">xml文件名字</param>
        /// <returns>SQL命令</returns>
        public string ParseXML(string strSearchName, string strXMLPath)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(strXMLPath);
                XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes("SqlText");
                foreach (XmlNode node in nodes)
                {
                    if (strSearchName == node.Attributes["Name"].Value)
                    {
                        return node.InnerText;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("xml解析失败！", ex);
            }
            return null;
        }


        /// <summary>
        /// 根据CommonType.xml文件的路径名和属性的名字解析出相应的属性
        /// </summary>
        /// <param name="strSearchName">属性的名字</param>
        /// <param name="strXMLPath">xml文件名字</param>
        /// <returns>SQL命令</returns>
        public ArrayList CommonTypeXML(string strSearchName, string strXMLPath)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(strXMLPath);
                XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes("Types");
                foreach (XmlNode node in nodes)
                {
                    if (node.Attributes["Name"].Value != strSearchName)
                    {
                        continue;
                    }
                    if (node != null && node.ChildNodes.Count > 0)
                    {
                        ArrayList arrayList = new ArrayList();
                        foreach (XmlNode tmpNode in node.ChildNodes)
                        {
                            arrayList.Add(new TypeList(tmpNode.Attributes["ID"].Value, tmpNode.InnerText));
                        }
                        return arrayList;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("xml解析失败！", ex);
            }
            return null;
        }

        /// <summary>
        /// 根据xml文件的路径名和命令的名字解析出相应的属性
        /// </summary>
        /// <param name="strSearchName">属性的名字</param>
        /// <param name="strXMLPath">xml文件名字</param>
        /// /// <param name="strParentID">ParentID属性值</param>
        /// <returns>SQL命令</returns>
        public ArrayList CommonTypeOIDXML(string strSearchName, string strXMLPath, string strParentID)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(strXMLPath);
                XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes("Types");
                foreach (XmlNode node in nodes)
                {
                    if (node.Attributes["Name"].Value != strSearchName)
                    {
                        continue;
                    }
                    if (node != null && node.ChildNodes.Count > 0)
                    {
                        ArrayList arrayList = new ArrayList();
                        if (strParentID != "")
                        {
                            foreach (XmlNode tmpNode in node.ChildNodes)
                            {
                                if (tmpNode.Attributes["ID"].Value.Trim() == strParentID)
                                {
                                    arrayList.Add(new TypeList(tmpNode.Attributes["ID"].Value, tmpNode.InnerText));
                                }
                            }
                        }
                        else
                        {
                            foreach (XmlNode tmpNode in node.ChildNodes)
                            {
                                arrayList.Add(new TypeList(tmpNode.Attributes["ID"].Value, tmpNode.InnerText));
                            }
                        }
                        return arrayList;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("xml解析失败！", ex);
            }
            return null;
        }


        /// <summary>
        /// 根据xml文件的路径名和命令的名字解析出相应的属性
        /// </summary>
        /// <param name="strSearchName">属性的名字</param>
        /// <param name="strXMLPath">xml文件名字</param>
        /// /// <param name="strParentID">ParentID属性值</param>
        /// <returns>SQL命令</returns>
        public ArrayList CommonTypeXML(string strSearchName, string strXMLPath, string strParentID)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(strXMLPath);
                XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes("Types");
                foreach (XmlNode node in nodes)
                {
                    if (node.Attributes["Name"].Value != strSearchName)
                    {
                        continue;
                    }
                    if (node != null && node.ChildNodes.Count > 0)
                    {
                        ArrayList arrayList = new ArrayList();
                        foreach (XmlNode tmpNode in node.ChildNodes)
                        {
                            arrayList.Add(new TypeList(tmpNode.Attributes[strParentID].Value, tmpNode.InnerText));
                        }
                        return arrayList;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("xml解析失败！", ex);
            }
            return null;
        }

        
 
    }

    /// <summary>
    /// 内部列表类
    /// </summary>
    public class TypeList
    {
        private string strID, strName;
        public TypeList(string id, string name)
        {
            strID = id;
            strName = name;
        }
        public string ID
        {
            get
            {
                return strID;
            }
        }
        public string Name
        {
            get
            {
                return strName;
            }
        }
    }
}
